package at.ac.uniklu.rallyemaster.adapters;

import android.content.Context;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;
import at.ac.uniklu.rallyemaster.RallyeMaster;

public class RallyeDBAdapter {
 
	  private static SQLiteDatabase db;
	  private final Context context;
	  private RallyeDBOpenHelper dbHelper;

	  public RallyeDBAdapter(Context _context) {
	    this.context = _context;
	    dbHelper = new RallyeDBOpenHelper(context, DBConstants.DATABASE_NAME, 
	                                    null, DBConstants.DATABASE_VERSION);
	  }
	  
	  public void close() {
	    db.close();
	  }
	  
	  public void open() throws SQLiteException {  
	    try {
	      db = dbHelper.getWritableDatabase();
	      RallyeMaster.logDebug("Writable Database was found");
	    } catch (SQLiteException ex) {
	    	
	      db = dbHelper.getReadableDatabase();
	      RallyeMaster.logDebug("Readable Database was found");
	      
	    }
	  }  
	  
	  
	  /**
	   * To get the database
	   * @return the database
	   */
	  public static SQLiteDatabase getDatabase(){
		  return db;
	  }
	  
	  private static class RallyeDBOpenHelper extends SQLiteOpenHelper {

		  public RallyeDBOpenHelper(Context context, String name,
		                          CursorFactory factory, int version) {
		    super(context, name, factory, version);
		  }

		  // SQL Statement to create a new database.
		  private static final String DATABASE_CREATE_TRACKINGPOINT = "create table " + 
		  DBConstants.DATABASE_TABLE_TRACKINGPOINT + " (" + DBConstants.KEY_POINT_ID + " integer primary key autoincrement, " +
		  DBConstants.KEY_POINT_LONG + " real not null, "+
		  DBConstants.KEY_POINT_LAT + " real not null,  " +
		  DBConstants.KEY_POINT_NOTES + " text , "  +
		  DBConstants.KEY_POINT_DISTANCE + " real , "  +
		  DBConstants.KEY_POINT_SPEED + " real , "  +
		  DBConstants.KEY_POINT_AVG_SPEED + " real , "  +
		  DBConstants.KEY_POINT_TIME + " numeric , "  +
		  DBConstants.KEY_POINT_CURVE + " numeric , "  +
		  DBConstants.KEY_POINT_CURVE_DIRECTION + " integer, " +
		  DBConstants.KEY_POINT_CURVE_INTENSION + " integer, "  +
		  DBConstants.KEY_POINT_TRACK_ID + " integer not null );";
		  
		  private static final String DATABASE_CREATE_TRACK = "create table " + 
		  DBConstants.DATABASE_TABLE_TRACK + " (" + DBConstants.KEY_TRACK_ID + " integer primary key autoincrement, " +
		  DBConstants.KEY_TRACK_NAME + " text not null, "  +
		  DBConstants.KEY_TRACK_NOTES + " text not null );";
		  		  
		  
		  @Override
		  public void onCreate(SQLiteDatabase _db) {
		    _db.execSQL(DATABASE_CREATE_TRACKINGPOINT);
		    _db.execSQL(DATABASE_CREATE_TRACK);
		  }

		  @Override
		  public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) {
		    Log.w("TrackDBAdapter", "Upgrading from version " + 
		                           _oldVersion + " to " +
		                           _newVersion + ", which will destroy all old data");

		    // Drop the old table.
		    _db.execSQL("DROP TABLE IF EXISTS " + DBConstants.DATABASE_TABLE_TRACKINGPOINT);
		    _db.execSQL("DROP TABLE IF EXISTS " + DBConstants.DATABASE_TABLE_TRACK);
		    // Create a new one.
		    onCreate(_db);
		  }
	}
}

